//------------------------------------------------------------------------------
// <copyright file="KeyBindingsControl.xaml.cs" company="Damian Antonowicz">
//       copyright © 2010 Damian Antonowicz
// </copyright>
// <author>Damian Antonowicz</author>
// <email>poczta@damianantonowicz.pl</email>
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DotBeer.Data.Properties;
using DAnton.Utilities.Helpers.KeyBindings;
using DAnton.Utilities.Extensions;
using System.Diagnostics;
using System.Reflection;
using System.ComponentModel;

namespace DotBeer.UI.WPFClient
{
    /// <summary>
    /// Interaction logic for KeyBindingsControl.xaml
    /// </summary>
    public partial class KeyBindingsControl : UserControl, IOptionsControl
    {
        #region private members

        IList<SerializableKeyGesture> gestures;
        IList<SerializableKeyGesture> gesturesCopy;

        #endregion

        #region constructors

        public KeyBindingsControl()
        {
            InitializeComponent();
            this.LoadBindings();
        }

        #endregion

        #region public methods

        #region IOptionsControl Members

        public bool Save()
        {
            try
            {
                for (int i = 0; i < this.gestures.Count; i++)
                {
                    this.gestures[i].Key = this.gesturesCopy[i].Key;
                    this.gestures[i].Modifier = this.gesturesCopy[i].Modifier;
                    this.gestures[i].Description = this.gesturesCopy[i].Description;
                    this.gestures[i].OwnerWindowName = this.gesturesCopy[i].OwnerWindowName;
                }

                WindowsBindingsSettings.Default.Save();
            }
            catch
            {
                WindowsBindingsSettings.Default.Reload();
                WindowsBindingsSettings.Default.Save();

                return false;
            }

            return true;
        }

        public void Cancel()
        {
        }

        #endregion

        #endregion

        #region private methods

        private void LoadBindings()
        {
            this.gestures = new List<SerializableKeyGesture>();
            this.gestures.Add(WindowsBindingsSettings.Default.WindowMainBindingShowSentinelCommand);
            this.gestures.Add(WindowsBindingsSettings.Default.WindowMainBindingShowNeutralCommand);
            this.gestures.Add(WindowsBindingsSettings.Default.WindowMainBindingShowScourgeCommand);
            this.gestures.Add(WindowsBindingsSettings.Default.WindowMainBindingAddNewBuildCommand);
            this.gestures.Add(WindowsBindingsSettings.Default.WindowMainBindingRefreshCommand);
            this.gestures.Add(WindowsBindingsSettings.Default.WindowMainBindingShowEditWindowCommand);
            this.gestures.Add(WindowsBindingsSettings.Default.WindowMainBindingShowOptionsWindowCommand);
            this.gestures.Add(WindowsBindingsSettings.Default.WindowMainBindingShowPrintWindowCommand);
            this.gestures.Add(WindowsBindingsSettings.Default.WindowEditBindingAddNewBuildCommand);
            this.gestures.Add(WindowsBindingsSettings.Default.WindowEditBindingSaveBuildCommand);
            this.gestures.Add(WindowsBindingsSettings.Default.WindowOptionsBindingSaveCommand);
            this.gestures.Add(WindowsBindingsSettings.Default.GlobalBindingHelpCommand);

            this.gesturesCopy = this.gestures.Clone();

            ICollectionView view = CollectionViewSource.GetDefaultView(this.gesturesCopy);
            view.GroupDescriptions.Add(new PropertyGroupDescription("OwnerWindowName"));

            this.ListBoxBindings.ItemsSource = view;
        }

        #endregion
    }
}
